1. /* combD.cpp by K.Tsuru */
  2. /**************************************
  3. It provides a binomial coefficient nCk
  4. double version. When n < nMax_combD(=48) does not overflow.
  5. See combL(n,k) in "slfcombl.cpp" for argorithm.
  6. **************************************/
  7. #include "tools.h"
  8. #define USE_modf_IN_combD 0
  9. double combD(ulong n, ulong k){
  10. if(n > nMax_combD) return 0.0;
  11. if(n < k) return 0.0;
  12. k = (n < n - k) ? n : n-k; // if n==k, k becomes 0.
  13. if(k == 0) return 1.0; // includes the case n == k
  14. #if USE_modf_IN_combD
  15. double u = n; // work variable in double
  16. double umax = INT_MAX_DBL; // = 10^DOUBLE_FIG
  17. ulong j;
  18. for(j = 1; j< k; j++){
  19. u *= n -j;
  20. if(u >= umax) return 0.0; // overflow
  21. u /= j+1;
  22. }
  23. double ip; // integer part
  24. if(modf(u, &ip) > 0.0) return 0.0; // not integer
  25. return ip;
  26. #else
  27. double u = n; // work variable in double
  28. ulong j;
  29. for(j = 1; j< k; j++){
  30. u *= n -j;
  31. u /= j+1;
  32. }
  33. return u;
  34. #endif
  35. }

combD.cpp : last modifiled at 2016/10/18 20:37:28(969 bytes)
created at 2016/10/29 15:19:40
The creation time of this html file is 2017/10/07 10:54:15 (Sat Oct 07 10:54:15 2017).